home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / kermit / ckufio.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  29KB  |  1,098 lines

  1. char *ckzv = "Unix file support, 4E(037) 27 Jan 88";
  2.  
  3. /* C K U F I O  --  Kermit file system support for Unix systems */
  4.  
  5. /* 4E, conditionals added for Apollo Aegis. */
  6.  
  7. /*
  8.  Author: Frank da Cruz (SY.FDC@CU20B),
  9.  Columbia University Center for Computing Activities, January 1985.
  10.  Copyright (C) 1985, Trustees of Columbia University in the City of New York.
  11.  Permission is granted to any individual or institution to use, copy, or
  12.  redistribute this software so long as it is not sold for profit, provided this
  13.  copyright notice is retained. 
  14. */
  15. /* Includes */
  16.  
  17. #include <sys/types.h>            /* Data types */
  18. #include "ckcker.h"            /* Kermit definitions */
  19. #include "ckcdeb.h"            /* Typedefs, debug formats, etc */
  20. #include <ctype.h>            /* Character types */
  21. #include <stdio.h>            /* Standard i/o */
  22. #ifdef MINIX
  23. #include <limits.h>
  24. #endif
  25. #include <sys/dir.h>            /* Directory structure */
  26. #include <pwd.h>            /* Password file for shell name */
  27.  
  28. #ifdef CIE
  29. #include <stat.h>            /* File status */
  30. #else
  31. #include <sys/stat.h>
  32. #endif
  33.  
  34.  
  35. /* Berkeley Unix Version 4.x */
  36. /* 4.1bsd support added by Charles E Brooks, EDN-VAX */
  37.  
  38. #ifdef BSD4
  39. #ifdef MAXNAMLEN
  40. #define BSD42
  41. char *ckzsys = " 4.2 BSD";
  42. #else
  43. #ifdef FT18
  44. #define BSD41
  45. char *ckzsys = " Fortune For:Pro 1.8";
  46. #else
  47. #define BSD41
  48. char *ckzsys = " 4.1 BSD";
  49. #endif
  50. #endif
  51. #endif
  52.  
  53. /* 2.9bsd support contributed by Bradley Smith, UCLA */
  54. #ifdef BSD29
  55. char *ckzsys = " 2.9 BSD";
  56. #endif
  57.  
  58. /* Version 7 Unix  */
  59. #ifdef V7
  60. char *ckzsys = " Version 7 Unix";
  61. #endif
  62.  
  63. /* DEC Professional-300 series with Venturcom Venix v1 */
  64. #ifdef PROVX1
  65. char *ckzsys = " DEC Pro-3xx/Venix v1";
  66. #endif
  67.  
  68. /* NCR Tower support contributed by John Bray, Auburn, AL. */
  69. /* Tower OS is like Sys III but with BSD features -- mostly follows BSD. */
  70. #ifdef TOWER1
  71. char *ckzsys = " NCR Tower 1632, OS 1.02";
  72. #endif
  73.  
  74. /* Sys III/V, Xenix, PC/IX,... support by Herm Fischer, Litton Data Systems */
  75. #ifdef UXIII
  76. #ifdef XENIX
  77. char *ckzsys = " Xenix/286";
  78. #else
  79. #ifdef PCIX
  80. char *ckzsys = " PC/IX";
  81. #else
  82. #ifdef ISIII
  83. char *ckzsys = " Interactive Systems Corp, System III";
  84. #else
  85. #ifdef ZILOG
  86. char *ckzsys = " Zilog S8000 Zeus 3.21+";
  87. #else
  88. char *ckzsys = " AT&T System III/System V";
  89. #endif
  90. #endif
  91. #endif
  92. #endif
  93. #endif
  94.  
  95. /* Definitions of some Unix system commands */
  96.  
  97. char *DELCMD = "rm -f ";        /* For file deletion */
  98. char *PWDCMD = "pwd ";            /* For saying where I am */
  99.  
  100. #ifdef FT18
  101. char *DIRCMD = "ls -l | more ";        /* For directory listing */
  102. char *TYPCMD = "more ";            /* For typing a file */
  103. #else
  104. char *TYPCMD = "cat ";            /* For typing a file */
  105. char *DIRCMD = "ls -l ";        /* For directory listing */
  106. #endif
  107.  
  108. #ifdef FT18
  109. #undef BSD4
  110. #endif
  111.  
  112. #ifdef BSD4
  113. char *SPACMD = "pwd ; quota ; df .";    /* Space/quota of current directory */
  114. #else
  115. #ifdef FT18
  116. char #SPACMD = "pwd ; du ; df .";
  117. #else
  118. char *SPACMD = "df ";
  119. #endif
  120. #endif
  121.  
  122. char *SPACM2 = "df ";            /* For space in specified directory */
  123.  
  124. #ifdef FT18
  125. #define BSD4
  126. #endif
  127.  
  128. #ifdef BSD4
  129. char *WHOCMD = "finger ";        /* For seeing who's logged in */
  130. #else
  131. char *WHOCMD = "who ";            /* For seeing who's logged in */
  132. #endif
  133.  
  134. /*
  135.   Functions (n is one of the predefined file numbers from ckermi.h):
  136.  
  137.    zopeni(n,name)   -- Opens an existing file for input.
  138.    zopeno(n,name)   -- Opens a new file for output.
  139.    zclose(n)        -- Closes a file.
  140.    zchin(n,&c)      -- Gets the next character from an input file.
  141.    zsout(n,s)       -- Write a null-terminated string to output file, buffered.
  142.    zsoutl(n,s)      -- Like zsout, but appends a line terminator.
  143.    zsoutx(n,s,x)    -- Write x characters to output file, unbuffered.
  144.    zchout(n,c)      -- Add a character to an output file, unbuffered.
  145.    zchki(name)      -- Check if named file exists and is readable, return size.
  146.    zchko(name)      -- Check if named file can be created.
  147.    znewn(name,s)    -- Make a new unique file name based on the given name.
  148.    zdelet(name)     -- Delete the named file.
  149.    zxpand(string)   -- Expands the given wildcard string into a list of files.
  150.    znext(string)    -- Returns the next file from the list in "string".
  151.    zxcmd(cmd)       -- Execute the command in a lower fork.
  152.    zclosf()         -- Close input file associated with zxcmd()'s lower fork.
  153.    zrtol(n1,n2)     -- Convert remote filename into local form.
  154.    zltor(n1,n2)     -- Convert local filename into remote form.
  155.    zchdir(dirnam)   -- Change working directory.
  156.    zhome()          -- Return pointer to home directory name string.
  157.    zkself()         -- Kill self, log out own job.
  158.  */
  159.  
  160. #ifdef FT18
  161. #define PROVX1
  162. #endif
  163.  
  164. /* Which systems include <sys/file.h>... */
  165. #ifndef PROVX1
  166. #ifndef aegis
  167. #ifndef CIE
  168. #ifndef MINIX
  169. #ifndef XENIX
  170. /* Watch out, some versions of Xenix might need to do this include, */
  171. /* but reportedly SCO Xenix 2.2 on an 80x86 system does not. */
  172. #include <sys/file.h>            /* File access */
  173. #endif
  174. #endif
  175. #endif
  176. #endif
  177. #endif
  178.  
  179. #ifdef FT18
  180. #undef PROVX1
  181. #endif
  182.  
  183. /* Some systems define these symbols in include files, others don't... */
  184. #ifndef R_OK
  185. #define R_OK 4                /* For access */
  186. #endif
  187.  
  188. #ifndef W_OK
  189. #define W_OK 2
  190. #endif
  191.  
  192. #ifdef PROVX1
  193. #define MAXNAMLEN DIRSIZ        /* Max file name length */
  194. #endif
  195.  
  196. #ifdef UXIII
  197. #include <fcntl.h>
  198. #define MAXNAMLEN DIRSIZ
  199. #endif
  200.  
  201. #ifndef O_RDONLY
  202. #define O_RDONLY 000
  203. #endif
  204.  
  205. #ifndef MAXNAMLEN
  206. #define MAXNAMLEN 14            /* If still not defined... */
  207. #endif
  208.  
  209. #ifdef PROVX1
  210. #define MAXWLD 50            /* Maximum wildcard filenames */
  211. #else
  212. #ifdef BSD29
  213. #define MAXWLD 50            /* Maximum wildcard filenames */
  214. #else
  215. #define MAXWLD 500
  216. #endif
  217. #endif
  218.  
  219. /* Declarations */
  220.  
  221. FILE *fp[ZNFILS] = {             /* File pointers */
  222.     NULL, NULL, NULL, NULL, NULL, NULL, NULL };
  223.  
  224. static int pid;                    /* pid of child fork */
  225. static int fcount;            /* Number of files in wild group */
  226. static char nambuf[MAXNAMLEN+2];    /* Buffer for a filename */
  227. char *malloc(), *getenv(), *strcpy();    /* System functions */
  228. extern errno;                /* System error code */
  229.  
  230. static char *mtchs[MAXWLD],        /* Matches found for filename */
  231.      **mtchptr;                /* Pointer to current match */
  232.  
  233. /*  Z K S E L F  --  Kill Self: log out own job, if possible.  */
  234.  
  235. /* Note, should get current pid, but if your system doesn't have */
  236. /* getppid(), then just kill(0,9)...  */
  237.  
  238. zkself() {                /* For "bye", but no guarantee! */
  239. #ifdef PROVX1
  240.     return(kill(0,9));
  241. #else
  242. #ifdef V7
  243.     return(kill(0,9));
  244. #else
  245. #ifdef TOWER1
  246.     return(kill(0,9));
  247. #else
  248. #ifdef FT18
  249.     return(kill(0,9));
  250. #else
  251. #ifdef aegis
  252.     return(kill(0,9));
  253. #else
  254.     return(kill(getppid(),1));
  255. #endif
  256. #endif
  257. #endif
  258. #endif
  259. #endif
  260. }
  261.  
  262. /*  Z O P E N I  --  Open an existing file for input. */
  263.  
  264. zopeni(n,name) int n; char *name; {
  265.     debug(F111," zopeni",name,n);
  266.     debug(F101,"  fp","",(int) fp[n]);
  267.     if (chkfn(n) != 0) return(0);
  268.     if (n == ZSYSFN) {            /* Input from a system function? */
  269.         debug(F110," invoking zxcmd",name,0);
  270.     return(zxcmd(name));        /* Try to fork the command */
  271.     }
  272.     if (n == ZSTDIO) {            /* Standard input? */
  273.     if (isatty(0)) {
  274.         ermsg("Terminal input not allowed");
  275.         debug(F110,"zopeni: attempts input from unredirected stdin","",0);
  276.         return(0);
  277.     }
  278.     fp[ZIFILE] = stdin;
  279.     return(1);
  280.     }
  281.     fp[n] = fopen(name,"r");        /* Real file. */
  282.     debug(F111," zopeni", name, (int) fp[n]);
  283.     if (fp[n] == NULL) perror("zopeni");
  284.     return((fp[n] != NULL) ? 1 : 0);
  285. }
  286.  
  287. /*  Z O P E N O  --  Open a new file for output.  */
  288.  
  289. zopeno(n,name) int n; char *name; {
  290.     debug(F111," zopeno",name,n);
  291.     if (chkfn(n) != 0) return(0);
  292.     if ((n == ZCTERM) || (n == ZSTDIO)) {   /* Terminal or standard output */
  293.     fp[ZOFILE] = stdout;
  294.     debug(F101," fp[]=stdout", "", (int) fp[n]);
  295.     return(1);
  296.     }
  297.     fp[n] = fopen(name,"w");        /* A real file, try to open */
  298.     if (fp[n] == NULL) {
  299.         perror("zopeno can't open");
  300.     } else {
  301.     chown(name, getuid(), getgid());     /* In case set[gu]id */
  302.         if (n == ZDFILE) setbuf(fp[n],(char *)NULL); /* Debugging file unbuffered */
  303.     }
  304.     debug(F101, " fp[n]", "", (int) fp[n]);
  305.     return((fp[n] != NULL) ? 1 : 0);
  306. }
  307.  
  308. /*  Z C L O S E  --  Close the given file.  */
  309.  
  310. /*  Returns 0 if arg out of range, 1 if successful, -1 if close failed.  */
  311.  
  312. zclose(n) int n; {
  313.     int x;
  314.     if (chkfn(n) < 1) return(0);    /* Check range of n */
  315.     if ((